home *** CD-ROM | disk | FTP | other *** search
-
- /*
- SimpleInMovies
-
- Sample programs demonstrating how to open and display
- QuickTime™ Movies.
-
- SimpleInUtils.c file contains the code for the routines
- called upon when dealing with the dialogs used to save movies.
- These calls came almost untouched from the DTS Utilities files,
- note though that some were edited to make them fit the needs of this
- sample.
-
- Guillermo A. Ortiz
- Macintosh Developer Technical Support
-
- 08/13/91 Created the file to move in utilities calls.
- 11/19/91 Added stuff to have custom dialog in PutFile to handle flattening.
- */
-
- #include <SimpleInMovie.h>
-
- long gFlattenMovies = 1;
-
- DialogPtr GetCenteredDialog(short, WindowPtr, WindowPtr);
- void OutlineDialogItem(DialogPtr, short);
- pascal Boolean keyEquivFilter(DialogPtr, EventRecord *, short *);
- Handle GetAppResource(ResType,short, OSErr *);
- Boolean DisplayPutFile(StandardFileReply *);
- void OutlineControl(ControlHandle);
- Rect CenterWindow(WindowPtr, WindowPtr);
- short GetHexByte(char *);
- char LockHandleHigh(Handle);
- GDHandle GetRectDevice(Rect);
- Rect GetWindowDeviceRectNMB(WindowPtr);
- Rect GetWindowDeviceRect(WindowPtr window);
- Rect GetMainScreenRect(void);
- Rect GetWindowStructureRect(WindowPtr);
- Point GetGlobalTopLeft(WindowPtr);
- void PositionRectInRect(Rect, Rect *, Fixed , Fixed);
- Rect GetWindowContentRect(WindowPtr window);
- void LocalToGlobalRect(Rect *);
- void pstrcat(char *, char *);
- void pstrcpy(char *, char *);
-
- /*****************************************************************************/
-
- DialogPtr GetCenteredDialog(short id, WindowPtr relatedWindow, WindowPtr behind)
- {
- DialogTHndl dlogResource;
- DialogPtr dialog;
- Boolean oldVis;
- char hstate;
- OSErr err;
-
- dialog = nil;
- if (dlogResource = (DialogTHndl)GetAppResource('DLOG', id, &err)) {
- hstate = LockHandleHigh((Handle)dlogResource);
- oldVis = (*dlogResource)->visible;
- (*dlogResource)->visible = false;
- if (dialog = GetNewDialog(id, nil, behind)) {
- CenterWindow(dialog, relatedWindow);
- if (oldVis) ShowWindow(dialog);
- }
- (*dlogResource)->visible = oldVis;
- HSetState((Handle)dlogResource, hstate);
- }
- return(dialog);
- }
-
- Handle GetAppResource(ResType theType,short theID, OSErr *err)
- {
- Handle returnHandle;
-
- returnHandle = Get1Resource(theType, theID);
- *err = ResError();
- return (returnHandle);
- }
-
- /* The dialog hook proc processes the item hits in the custom put dialog;
- this sample does the following:
-
- on sfHookFirstCall : initializes the 'Flatten Movie' check box, the
- global flag controlling this starts with YES, flatten.
- Return sfHookNullEvent to signal the hit has been taken
- care of.
-
- on kFlattenMovie : the user clicked on the box change it to the new state.
- Return sfHookNullEvent to signal the hit has been taken
- care of.
-
- on sfItemOpenButton : the user says ok, save the state of the flatten box
- so the next time it is used to init the dialog.
- Return sfItemOpenButton to let SF do its normal stuff.
- */
- pascal short MyDlogHook(short item, DialogPtr theDialog, Ptr myData)
- {
- short itemType, value;
- Handle dItem;
- Rect box;
-
- switch (item) {
- case sfHookFirstCall:
- if (*(long *)myData) { /* check if flatten movie has been selected and mark box accordingly */
- GetDItem(theDialog, kFlattenMovie, &itemType, &dItem, &box);
- if (itemType == (ctrlItem+chkCtrl)) { /* is this our item? */
- SetCtlValue((ControlHandle)dItem, 1); /* check the box, comes unchecked from resource */
- }
- }
- return sfHookNullEvent;
- break;
-
- case kFlattenMovie:
- GetDItem(theDialog, kFlattenMovie, &itemType, &dItem, &box);
- if (itemType == (ctrlItem+chkCtrl)) { /* is this our item? */
- value = GetCtlValue((ControlHandle)dItem); /* get current setting */
- SetCtlValue((ControlHandle)dItem, value?0:1); /* and switch it */
- }
- return sfHookNullEvent;
- break;
- case sfItemOpenButton:
- GetDItem(theDialog, kFlattenMovie, &itemType, &dItem, &box);
- if (itemType == (ctrlItem+chkCtrl)) { /* is this our item? */
- value = GetCtlValue((ControlHandle)dItem); /* get current setting */
- *(long *)myData =value;
- }
- break;
- default:
- break;
- }
- return item; /* in all other cases return the item to let SF do the normal processing */
- }
-
- Boolean DisplayPutFile(reply)
- StandardFileReply *reply;
- {
- Str255 prompt;
- Point where = {-1, -1};
-
- GetIndString(prompt, rMiscStrings, sSFprompt);
-
- CustomPutFile(prompt, reply->sfFile.name, reply, rCustomPut, where,
- MyDlogHook, nil, nil, nil, &gFlattenMovies);
-
- return(reply->sfGood);
- }
-
- void OutlineDialogItem(DialogPtr dlgPtr, short item)
- {
- short iKind;
- Handle iHandle;
- Rect iRect;
-
- GetDItem (dlgPtr, item, &iKind, &iHandle, &iRect);
- OutlineControl((ControlHandle) iHandle);
- }
-
- void OutlineControl(ControlHandle button)
- {
- Rect theRect;
- PenState curPen;
- short buttonOval;
-
- if (button) {
- SetPort((*button)->contrlOwner);
- GetPenState(&curPen);
- PenNormal();
- theRect = (*button)->contrlRect;
- InsetRect(&theRect, kButtonFrameInset, kButtonFrameInset);
- buttonOval = (theRect.bottom - theRect.top) / 2 + 2;
-
- PenPat((*button)->contrlHilite == kCntlActivate ? &(qd.black) : &(qd.gray));
-
- PenSize(kButtonFrameSize, kButtonFrameSize);
- FrameRoundRect(&theRect, buttonOval, buttonOval);
- SetPenState(&curPen);
- }
- }
-
-
- pascal Boolean keyEquivFilter(DialogPtr dlg, EventRecord *event, short *item)
- {
- short itemType;
- Handle itemHndl;
- Rect itemRect;
- Str255 itemText;
- short i, theChr, theMod, equivChr, modMask, modVal, itemNum;
- long tick;
-
- if (event->what == updateEvt) {
- if (dlg == (DialogPtr)event->message) OutlineDialogItem(dlg, 1);
- return(false);
- }
-
- if (event->what != keyDown) return(false);
-
- itemNum = 0;
-
- theChr = event->message & charCodeMask;
- theMod = event->modifiers & keyCodeMask;
-
- if ((theChr == 0x0D) || (theChr == 0x03)) { /* If return or enter... */
- if (!(theMod & (cmdKey + optionKey + controlKey))) itemNum = 1;
- }
- else {
-
- GetDItem(dlg, 2, &itemType, &itemHndl, &itemRect);
- GetIText(itemHndl, itemText);
-
- for (i = 1; i <= *itemText; i += 9) {
- equivChr = GetHexByte(itemText + i);
- modMask = GetHexByte(itemText + i + 2) << 8;
- modVal = GetHexByte(itemText + i + 4) << 8;
- itemNum = GetHexByte(itemText + i + 6);
- if (theChr == equivChr)
- if ((theMod & modMask) == modVal) break;
- itemNum = 0;
- }
- }
-
- if (itemNum) {
- GetDItem(dlg, itemNum, &itemType, &itemHndl, &itemRect);
- HiliteControl((ControlHandle)itemHndl, 1);
- tick = TickCount();
- while (TickCount() < tick + 10);
- HiliteControl((ControlHandle)itemHndl, 0);
- *item = itemNum;
- return(true);
- }
-
- return(false);
- }
-
- Rect GetWindowDeviceRectNMB(WindowPtr window)
- {
- Rect deviceRect, tempRect;
-
- deviceRect = GetWindowDeviceRect(window);
- tempRect = GetMainScreenRect();
- if (EqualRect(&deviceRect, &tempRect)) deviceRect.top += GetMBarHeight();
-
- return(deviceRect);
- }
-
- Rect CenterWindow(WindowPtr window, WindowPtr relatedWindow)
- {
- WindowPtr whichDevice;
- Rect deviceRect, oldWindowRect, newWindowRect, contentRect;
-
- if (!(whichDevice = relatedWindow)) whichDevice = window;
- /* If we have a window to center against, use the device for that window,
- ** else use the device for the window that is getting centered. */
-
- deviceRect = GetWindowDeviceRectNMB(whichDevice);
- /* We now have the rectangle of the device we want to center within. */
-
- oldWindowRect = newWindowRect = GetWindowStructureRect(window);
-
- PositionRectInRect(deviceRect, &newWindowRect, FixRatio (1, 2),
- FixRatio (1, 3));
- /* Figure out the new window strucRect so we can compare it against
- ** the old strucRect. This will tell us how much to move the window. */
-
- contentRect = GetWindowContentRect(window);
- /* Get where the window is now. */
-
- OffsetRect(&contentRect, newWindowRect.left - oldWindowRect.left,
- newWindowRect.top - oldWindowRect.top);
- /* Calculate the new content rect. */
-
- MoveWindow(window, contentRect.left, contentRect.top, false);
- /* Move the window to the new location. */
-
- return(contentRect);
- }
-
- GDHandle GetRectDevice(Rect globalRect)
- {
- long area;
- long maxArea;
- GDHandle device;
- GDHandle deviceToReturn;
- Rect intersection;
-
- deviceToReturn = GetMainDevice(); /* Use as default choice. */
- maxArea = 0;
-
- for (device = GetDeviceList(); device; device = GetNextDevice(device)) {
- if (TestDeviceAttribute(device, screenDevice)
- && TestDeviceAttribute(device, screenActive)
- && SectRect(&globalRect, &((*device)->gdRect), &intersection)) {
- area = (intersection.right - intersection.left) *
- (intersection.bottom - intersection.top);
- if (area > maxArea) {
- deviceToReturn = device;
- maxArea = area;
- }
- }
- }
- return(deviceToReturn);
- }
-
- Rect GetWindowDeviceRect(WindowPtr window)
- {
- #pragma unused (window)
-
- return(GetMainScreenRect());
- }
-
- Rect GetMainScreenRect(void)
- {
- GDHandle mainDevice;
-
- mainDevice = GetMainDevice();
- return((*mainDevice)->gdRect);
- }
-
- short GetHexByte(char *cptr)
- {
- short val, i, chr;
-
- for (val = 0, i = 0; i < 2; ++i) {
- chr = cptr[i];
- if (chr == '=') return(cptr[++i]);
- if (chr > 'F') chr -= 0x20;
- if (chr > '9') chr -= ('A' - '9' - 1);
- val = (val << 4) + chr - '0';
- }
- return(val);
- }
-
- char LockHandleHigh(Handle theHandle)
- {
- char hstate;
-
- hstate = HGetState(theHandle);
- MoveHHi(theHandle);
- HLock(theHandle);
- return(hstate);
- }
-
- Rect GetWindowStructureRect(WindowPtr window)
- {
- #define kOffscreenLoc 0x4000
-
- GrafPtr oldPort;
- Rect structureRect;
- Point windowLoc;
-
- if (((WindowPeek)window)->visible)
- structureRect = (*(((WindowPeek)window)->strucRgn))->rgnBBox;
-
- else {
- GetPort(&oldPort);
- SetPort(window);
- windowLoc = GetGlobalTopLeft(window);
- MoveWindow(window, windowLoc.h, kOffscreenLoc, false);
- ShowHide(window, true);
- structureRect = (*(((WindowPeek)window)->strucRgn))->rgnBBox;
- ShowHide(window, false);
- MoveWindow(window, windowLoc.h, windowLoc.v, false);
- SetPort(oldPort);
- OffsetRect(&structureRect, 0, windowLoc.v - kOffscreenLoc);
- }
- return(structureRect);
- }
-
- Point GetGlobalTopLeft(WindowPtr window)
- {
- GrafPtr oldPort;
- Point globalPt;
-
- GetPort(&oldPort);
- SetPort(window);
- globalPt = TopLeft(window->portRect);
- LocalToGlobal(&globalPt);
- SetPort(oldPort);
- return(globalPt);
- }
-
- void PositionRectInRect(Rect outerRect, Rect *innerRect, Fixed horzRatio, Fixed vertRatio)
- {
- short outerRectHeight;
- short outerRectWidth;
- short innerRectHeight;
- short innerRectWidth;
- short yLocation;
- short xLocation;
-
- outerRectHeight = outerRect.bottom - outerRect.top;
- outerRectWidth = outerRect.right - outerRect.left;
-
- innerRectHeight = innerRect->bottom - innerRect->top;
- innerRectWidth = innerRect->right - innerRect->left;
- yLocation = Fix2Long(FixMul(Long2Fix(outerRectHeight - innerRectHeight), vertRatio))
- + outerRect.top;
- xLocation = Fix2Long(FixMul(Long2Fix(outerRectWidth - innerRectWidth), horzRatio))
- + outerRect.left;
-
- innerRect->top = yLocation;
- innerRect->left = xLocation;
- innerRect->bottom = yLocation + innerRectHeight;
- innerRect->right = xLocation + innerRectWidth;
- }
-
- Rect GetWindowContentRect(WindowPtr window)
- {
- WindowPtr oldPort;
- Rect contentRect;
-
- GetPort(&oldPort);
- SetPort(window);
- contentRect = window->portRect;
- LocalToGlobalRect(&contentRect);
- SetPort(oldPort);
- return(contentRect);
- }
-
- void LocalToGlobalRect(Rect *aRect)
- {
- LocalToGlobal(&TopLeft(*aRect));
- LocalToGlobal(&BotRight(*aRect));
- }
-
- void pstrcat(char *d, char *s)
- {
- short i;
-
- for (i = 0; i < s[0];)
- d[++d[0]] = s[++i];
- }
-
- void pstrcpy(char *d, char *s)
- {
- short i;
-
- i = *s;
- do {
- d[i] = s[i];
- } while (i--);
- }
-
- /*****************************************************************************/
-